package com.ship.dispatch.bpm.result;

import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.orderPay.SpPaymentOrder;
import com.ship.dispatch.service.*;
import com.ship.dispatch.service.fob.FobService;
import com.ship.dispatch.service.orderPay.SpPaymentOrderService;
import com.ship.dispatch.service.shipBusiness.SmShipReportService;
import com.ship.system.api.ConBpmResultService;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class ProcessInstanceListener extends AbstractFlowableEngineEventListener {

    @Resource
    @Lazy
    ConBpmResultService conBpmResultService;

    @Resource
    @Lazy
    FobService fobService;

    @Resource
    @Lazy
    SpDispatchSchemeDetailService spDispatchSchemeDetailService;

    @Resource
    @Lazy
    SpDispatchService spDispatchService;

    @Resource
    @Lazy
    SpArrangeSchemeService spArrangeSchemeService;

    @Resource
    @Lazy
    SpPaymentOrderService spPaymentOrderService;

    @Resource
    @Lazy
    SmShipCrewService smShipCrewService;
    @Resource
    @Lazy
    SmShipReportService smShipReportService;
    @Resource
    @Lazy
    SpOverhaulService spOverhaulService;

    @Resource
    @Lazy
    TzFuelReportService tzFuelReportService;

    @Resource
    @Lazy
    private RuntimeService runtimeService;
    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {

        ExecutionEntityImpl processInstance = (ExecutionEntityImpl) event.getEntity();
        Map<String,Object> map =  processInstance.getProcessVariables();
        Object resolveTaskStatus = map.get("resolveTaskStatus");
        if(resolveTaskStatus!=null && "2".equals(resolveTaskStatus+"")){
            return;
        }
        runtimeService.setVariable(processInstance.getId(),"compleateVal", SecurityUtils.getUsername());
        if(processInstance.getProcessDefinitionKey().startsWith("con") || processInstance.getProcessDefinitionKey().startsWith("saveClause")){
            conBpmResultService.updateConResult(processInstance.getBusinessKey(),1,processInstance.getProcessDefinitionKey());
        }else if(processInstance.getProcessDefinitionKey().startsWith("spPayOrder")){

            String serviceType = map.get("serviceType").toString();
            fobService.updateApprovaResult(processInstance.getBusinessKey(),1,processInstance.getProcessDefinitionKey(),serviceType);
        } else if(processInstance.getProcessDefinitionKey().startsWith("spYearPlan")){
            spDispatchSchemeDetailService.yearPlanSchemeApproveBack(processInstance.getBusinessKey(),1);
        } else if(processInstance.getProcessDefinitionKey().startsWith("spDispatchScheme")){
            spDispatchService.dispatchSchemeApproveBack(processInstance.getBusinessKey(),1);
        } else if(processInstance.getProcessDefinitionKey().startsWith("arrangeSchemeApproveBack")){
            //Map<String,Object> map =  processInstance.getProcessVariables();
            spArrangeSchemeService.arrangeSchemeApproveBack(map,Long.parseLong(processInstance.getBusinessKey()),1);
        }else if(processInstance.getProcessDefinitionKey().startsWith("sysremDelete")) {
            //Map<String, Object> map = processInstance.getProcessVariables();
            String module = map.get("module").toString();
            if ("con".equals(module)) { // 合同类的删除
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                conBpmResultService.deleteConResult(processInstance.getBusinessKey(), 1, processInstance.getProcessDefinitionKey(),serviceType);
            } else if ("payOrder".equals(module)) {//订单类删除
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                if (serviceType == 1) {
                    spPaymentOrderService.removeById(processInstance.getBusinessKey());
                }
            } else if ("shipCrew".equals(module)) {
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                if (serviceType == 1) {
                    smShipCrewService.cancelDelete(processInstance.getBusinessKey(), 1);
                }
            }else if("shipReport".equals(module)) {
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                if (serviceType == 1) {
                    smShipReportService.cancelDelete(processInstance.getBusinessKey(), 1);
                }
            }else if("spOverhaul".equals(module)) {
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                if (serviceType == 1) {
                    spOverhaulService.cancelDelete(processInstance.getBusinessKey(), 1);
                }
            }else if("tzFuelReport".equals(module)) {
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                if (serviceType == 1) {
                    tzFuelReportService.dataDeleteRollback(processInstance.getBusinessKey(),1);
                }
            }else if("dispatchGoodsDelete".equals(module)) {
                Integer serviceType = Integer.parseInt(map.get("serviceType") + "");
                if (serviceType == 1) {
                    spDispatchSchemeDetailService.dataDeleteRollback(processInstance.getBusinessKey(),1);
                }
            }
         }
    }
}
